package com.common.viewlibrary.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

import java.util.List;

public abstract class BaseListAdapter<T,D extends BaseListAdapter.BaseViewHolder> extends BaseAdapter {

    protected Context context;
    protected List<T> list;

    public BaseListAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    protected abstract D getViewHolder();

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        D viewHolder;

        if (view == null) {
            viewHolder=getViewHolder();
            view = viewHolder.getView();
            view.setTag(viewHolder);
        } else {
            viewHolder= (D) view.getTag();
        }

        bindViewHolder(viewHolder,list.get(i));

        return view;
    }

    protected abstract void bindViewHolder(D viewHolder,T item);

   public class BaseViewHolder{
        View view;

        public BaseViewHolder(View view) {
            this.view = view;
        }

        public View getView() {
            return view;
        }

    }
}
